﻿using CATLTempUpdate.CommonLib;
using CATLTempUpdate.TcpServer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace CATLTempUpdate
{
    public partial class FormDataShow : Form
    {
        Thread datashowthd;
        public FormDataShow()
        {
            InitializeComponent();
            //TempCanData.TempDt.Rows.Clear();
            //SenseDataCollection.IfShowSenseData = true;

        }

        private void FormDataShow_Load(object sender, EventArgs e)
        {

            datashowthd = new Thread(ShowData);
            datashowthd.Start();
        }

        public void ShowData()
        {
            while (true)
            {

                this.Invoke(new Action(() =>
                {
                    TempDataDgv.DataSource = TempCanData.TempDt;
                }
                       ));
                Thread.Sleep(200);
            }
        }

        private void FormDataShow_FormClosing(object sender, FormClosingEventArgs e)
        {
            SenseDataCollection.IfShowSenseData = false;
            SenseDataCollection.IfWriteLog = false;
            datashowthd.Abort();
        }

        private void btnRefreshData_Click(object sender, EventArgs e)
        {

            if (btnRefreshData.Text == "开始刷新数据")
            {
                SenseDataCollection.IfShowSenseData = false;
                Thread.Sleep(100);
                //初始化数据
                try
                {
                    foreach (DataRow dr in TempCanData.TempDt.Rows)
                    {
                        dr["temp"] = "10";
                    }
                }
                catch (Exception ex)
                {

                }

                SenseDataCollection.IfShowSenseData = true;
                btnRefreshData.Text = "暂停刷新";
            }
            else
            {
                SenseDataCollection.IfShowSenseData = false;
                btnRefreshData.Text = "开始刷新数据";
            }

        }

        /// <summary>
        /// 字符串转16进制数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        private static byte[] strToToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }


        private void btnwritelog_Click(object sender, EventArgs e)
        {
            if (btnwritelog.Text == "开启日志")
            {
                SenseDataCollection.IfWriteLog = true;
                btnwritelog.Text = "关闭日志";
            }
            else
            {
                btnwritelog.Text = "开启日志";
                SenseDataCollection.IfWriteLog = false;
            }
        }

        private void TempDataDgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnwritelog_Click_1(object sender, EventArgs e)
        {
            if (btnwritelog.Text == "开启日志")
            {
                SenseDataCollection.IfWriteLog = true;
                btnwritelog.Text = "关闭日志";
            }
            else
            {
                btnwritelog.Text = "开启日志";
                SenseDataCollection.IfWriteLog = false;
            }
        }
    }
}
